package org.example.service;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.entity.Menus;
import org.example.entity.TRoleMenu;
import org.example.mapper.MenusMapper;
import org.example.mapper.TRoleMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2025-11-03
 */
@Service
public class MenusServiceImpl extends ServiceImpl<MenusMapper, Menus> {
    @Autowired
    private MenusMapper mapper;
    @Autowired
    private TRoleMenuMapper tRoleMenuMapper;

    public List<Menus> selectByRoleId(Long roleId) {
        List<TRoleMenu> tRoleMenus = tRoleMenuMapper
                .selectList(Wrappers.<TRoleMenu>lambdaQuery()
                        .eq(TRoleMenu::getRoleId, roleId));
        List<Menus> menus = Collections.emptyList();
        if (CollectionUtils.isNotEmpty(tRoleMenus)) {
            menus = mapper.selectList(Wrappers.<Menus>lambdaQuery()
                    .in(Menus::getId, tRoleMenus.stream()
                            .map(TRoleMenu::getMenuId)
                            .collect(Collectors.toList())));
        }
        return menus;
    }

}
